package cn.ulyer.core.modules.system.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.ulyer.common.jpa.entity.Dept;
import cn.ulyer.common.jpa.entity.QDept;
import cn.ulyer.common.jpa.service.AbstractService;
import cn.ulyer.core.modules.system.service.TDeptService;
import cn.ulyer.core.modules.system.vo.DeptVo;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TDeptDeptServiceImpl extends AbstractService<Dept,Long> implements TDeptService {

    @Override
    public List<DeptVo> listDeptVo(Dept dept) {
        QDept qDept = QDept.dept;
        JPAQuery<DeptVo> typeQuery = jpaQueryFactory
                .select(
                        Projections.bean(DeptVo.class,qDept.id,
                                qDept.name,qDept.status,qDept.parentId,qDept.createTime,qDept.updateTime
                )).from(qDept);
        if(dept!=null){
            if(StrUtil.isNotBlank(dept.getName())){
                typeQuery.where(qDept.name.like(dept.getName()));
            }
            if(dept.getStatus()!=null){
                typeQuery.where(qDept.status.eq(dept.getStatus()));
            }
        }
        typeQuery.orderBy(qDept.createTime.desc());
        return typeQuery.fetch();
    }


}
